<?php

namespace app\sys\controller;

use app\common\model\MenuModel;
use app\common\model\PermissionModel;
use app\common\model\RoleModel;
use think\Controller;
use think\Db;
use think\Request;

class Role extends Base
{
    public function index(Request $request)
    {
        if (!$request->isAjax()) {
            return view();
        } else {
            $params = $request->param();
            $search = $params['search'];
            $sort = $params['sort'];
            $order = $params['order'];
            $offset = $params['offset'];
            $limit = $params['limit'];

            //拼接查询语句
            $model = RoleModel::where('1', '=', '1');

            if ($search != null && $search !== '') {
                $model = $model->where('name|code|represent', 'like', "%$search%");
            }

            $model = $model->order($sort, $order);
            $model = $model->limit($offset, $limit);

            $data['rows'] = $model->select();
            $data['total'] = $model->count();

            $data['search'] = $search;
            $data['sort'] = $sort;
            $data['order'] = $order;
            $data['offset'] = $offset;
            $data['limit'] = $limit;
            return json($data);
        }
    }

    public function add(Request $request)
    {
        if (!$request->isAjax()) {
            return view();
        } else {
            $role = new RoleModel(
                [
                    'name' => $request->param('name'),
                    'code' => $request->param('code'),
                    'represent' => $request->param('represent'),
                    'user_id' => session('loginUser')->id
                ]
            );

            $roles = RoleModel::where('code', '=', $role['code'])->select();
            if (count($roles) > 0) {
                return json([
                    'status' => false,
                    'msg' => 'code 不可用',
                    'role' => $role
                ]);
            }

            $role->save();
            return json([
                'status' => true,
                'msg' => '添加成功',
                'role' => $role
            ]);
        }
    }

    public function edit(Request $request)
    {
        if (!$request->isAjax()) {
            $id = $request->param('id');
            $role = RoleModel::where('id', '=', $id)->find();
            if ($role == null) {
                exit("<script type='text/javascript'>alert('对象不存在或已被删除');window.history.back(-1);</script>");
            }
            $data['role'] = $role;
            return view('', $data);
        } else {
            $id = $request->param('id');
            $role = RoleModel::where('id', '=', $id)->find();
            if ($role == null) {
                return json([
                    'status' => false,
                    'msg' => '对象不存在或已被删除'
                ]);
            }

            $role['name'] = $request->param('name');
            $role['code'] = $request->param('code');
            $role['represent'] = $request->param('represent');
            $role['update_time'] = date('Y-m-d H:i:s', time());

            $roles = RoleModel::where('code', '=', $role['code'])->where('id', 'notin', $id)->select();
            if (count($roles) > 0) {
                return json([
                    'status' => false,
                    'msg' => 'code 不可用',
                    'role' => $role
                ]);
            }
            $role->save();
            return json([
                'status' => true,
                'msg' => '修改成功！！！',
                'role' => $role
            ]);
        }
    }

    public function delete(Request $request)
    {
        $ids = $request->param('ids');
        if ($ids == null) {
            return json([
                'status' => false,
                'msg' => '请传递要删除的IDS'
            ]);
        }

        $idsArray = explode('-', $ids);

        Db::transaction(function () use ($idsArray) {
            foreach ($idsArray as $id) {
                PermissionModel::where('role_id', '=', $id)->delete();
                RoleModel::where('id', '=', $id)->delete();
            }
        });

        return json([
            'status' => true,
            'msg' => '删除成功',
            'ids' => $idsArray
        ]);
    }

    public function roleSetting(Request $request)
    {
        if (!$request->isAjax()) {
            $id = $request->param('id');
            $role = RoleModel::where('id', '=', $id)->find();
            if ($role == null) {
                exit("<script type='text/javascript'>alert('对象不存在或已被删除');window.history.back(-1);</script>");
            }
            $data['role'] = $role;
            return view('', $data);
        } else {
            $role_id = $request->param('id');
            $menu_ids = $request->param('menuIds');
            $menus = explode('-', $menu_ids);

            Db::transaction(function () use ($role_id, $menus) {
                PermissionModel::where('role_id', '=', $role_id)->delete();
                foreach ($menus as $menu_id) {
                    $permission = new PermissionModel(
                        [
                            'role_id' => $role_id,
                            'menu_id' => $menu_id
                        ]
                    );
                    $permission->save();
                }
            });

            return json([
                'status' => true,
                'msg' => '设置成功'
            ]);
        }
    }


    public function get_menus(Request $request)
    {
        $role_id = $request->param('id');
        $menus = MenuModel::where('1', '=', '1')->select();
        foreach ($menus as $key => $menu) {
            $ps = PermissionModel::where('role_id', '=', $role_id)->where('menu_id', '=', $menu->id)->select();
            if (count($ps) > 0) {
                $menus[$key]['hasPermission'] = true;
            } else {
                $menus[$key]['hasPermission'] = false;
            }
        }
        $data['rows'] = $menus;
        return json($data);
    }
}
